import request from "umi-request";
import { router } from 'umi';
import { message} from 'antd';

const { pathname, search } = window.location;
// 获取url参数
const getQueryVariable = (variable) => {
    let query = search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] == variable) { return pair[1]; }
    }
    return (false);
}


// 测试环境  
// 授权地址 https://172.30.128.30:8082/idp/oauth2/authorize?redirect_uri=https://172.30.136.97:10081&state=s2&client_id=newGateway&response_type=code
// 接口地址 https://172.30.136.97:10081/api/auth/ssologin

// 正式环境 
// 授权地址 https://login.sichuanair.com/idp/oauth2/authorize?redirect_uri=https://newpssn.sichuanair.com&state=s2&client_id=scalwa&response_type=code
// 接口地址 https://172.30.136.97:10081/api/auth/ssologin

// 判断授权
if (search === '?state=s2') { //存在?state=s2参数，则认为是系统跳转进入，进行单点登录验证

    window.location.href ="https://login.sichuanair.com/idp/oauth2/authorize?redirect_uri=https://newpssn.sichuanair.com:10081/&state=s2&client_id=pssn&response_type=code"
    
    // request.get('https://172.30.128.30:8082/idp/oauth2/authorize', {
    //     params: {
    //         redirect_uri: "https://172.30.136.97:10081",
    //         state: "s2",
    //         client_id: "newGateway",
    //         response_type: "code",
    //     }
    // }).then(data => {
    //     console.log('data :>> ', data);
    // }).catch(err => {
    //     console.log('err :>> ', err);
    //     //   window.location.href = 'https://newpssn.sichuanair.com:10081'
    // })
} else { // 不存在code参数，判断本系统的登录token
    const isLogin = localStorage.getItem('authorization');
    if (!isLogin && pathname !== '/user/login') {
      localStorage.clear();
       router.push({
          pathname: "/user/login"
      });
    }
}

// 判断单点登录
if (getQueryVariable('code')) { //存在code参数，则认为是系统跳转进入，进行单点登录验证
    request.get('https://newpssn.sichuanair.com:10081/api/auth/ssologin', {
        params: {
            code: getQueryVariable('code'),
            state: getQueryVariable('state'),
        }
    }).then(data => {
        let resData = data?.data;
        if (resData) {
            localStorage.setItem('authorization', resData.authorization);
            localStorage.setItem('id', resData.id);
            localStorage.setItem('refresh', resData.refresh);
            localStorage.setItem('resource', JSON.stringify(resData.resource));
            localStorage.setItem('isFirstLogin', resData.isFirstLogin);
            localStorage.setItem('moduleMap', JSON.stringify(resData.moduleMap));
            localStorage.setItem('userId', resData.username);

            let initializingRoute = resData.resource[0].children[0].path;
            if (['/usualMonitor/usualMonitor/AuditDetail',
            '/unusualMonitor/unusualMonitor/AuditDetail',
            '/compensation/CompensationAudit/AuditDetail',
            '/compensation/indemnityForm',
            '/compensation/luggageLndemnifyDetails'].indexOf(initializingRoute) > -1) {
                initializingRoute = data.resource[0].children[1].path;
            }
            localStorage.setItem('initializingRoute', initializingRoute);
            // router.push({
            //     pathname: initializingRoute,
            // });
            window.location.href = initializingRoute;

        } else {
            message.error('登录失败,请重新登录!');
            // router.push({
            //     pathname: "/user/login"
            // });
            window.location.href = "/user/login";
        }
    }).catch(err => {
        // router.push({
        //     pathname: "/user/login"
        // });
        window.location.href = "/user/login";

    })
} else { // 不存在code参数，判断本系统的登录token
 const isLogin = localStorage.getItem('authorization');
  if (!isLogin && pathname !== '/user/login') {
    localStorage.clear();
    // router.push({
    //     pathname: "/user/login"
    // });
    window.location.href = "/user/login";
  }
}
